/* 
 *  <copyright file="DrawableText.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SobrietyEngine.Components.Graphic
{
    public class DrawableText : Base2DGraphic
    {
        SpriteFont Font;
        String text;
        Color color;
        IOrientation Rotation;
        IPosition Position;
        IScale Scale;

        float rot = 0.0f;
        float scale = 1.0f;
        Vector2 pos = new Vector2();

        public DrawableText()
            : base()
        {
        }

        public DrawableText(String _text)
        {
            text = _text;
            color = Color.Green;
            color.A = 100;
        }

        public DrawableText(String _text, Vector4 colorVec)
        {
            text = _text;
            color = new Color(colorVec);
            color.A = 100;
        }

         override public Boolean InitComponent()
        {
            Rotation = Actor.Get<IOrientation>();

            Position = Actor.Get<IPosition>();
            if (Position == null)
                throw new Exception("Position required");

            if (Font == null)
            {
                Font = Actor.GameServices().AssetGameService.Load<SpriteFont>("Fonts/menuFont");
            }

            Scale = Actor.Get<IScale>();

            return true;
        }

        
        public override void UpdateGraphic(GameTime time)
        {
            if (Rotation != null)
                rot = Rotation.GetRadiansXY();

            if (Scale != null)
                scale = (float)Scale.Scale.X;

            pos = Position.PositionV2;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (Visible == false)
                return;

            spriteBatch.DrawString(Font, text, 
                pos, 
                color /*Color*/,
                rot, 
                new Vector2(0,0),
                scale,
                SpriteEffects.None,
                1);
        }
    }
}